﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Antlr.Runtime;
using Antlr.Runtime.Tree;
using YAPL.Language.Ast;

namespace YAPL.Language.SourceParser.Antlr {
	public class AntlrInputParser : InputParser {
		public AntlrInputParser(InputInfo inputInfo) : base(inputInfo) {}
		public override Expression ParseInput() {
			try {
				var lex = new YaplLexer(new ANTLRFileStream(_inputInfo.InputFileName));
				var tokens = new CommonTokenStream(lex);
				var parser = new YaplParser(tokens);
				var prog = parser.prog();
				var tree = (CommonTree)prog.Tree;
				var nodes = new CommonTreeNodeStream(tree);
				var walker = new YaplTree(nodes);
				return walker.prog();
			}
			catch (Exception e) {
				throw new ParserException("Error parsing source code",e);
			}
		}
	}
}
